package cn.com.xf.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.TcaKnowledgefile;
import cn.com.xf.domain.TcaKnowledgefolder;
import cn.com.xf.service.WikiService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.XFMappingJacksonJsonView;
import cn.com.xf.web.form.BarForm;
import cn.com.xf.web.form.WikiForm;

@Controller
@RequestMapping("/wiki")
public class WikiController extends AbstractBaseController {
	@Autowired
	private WikiService wikiService;

	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, BarForm form) throws Exception {
		form.setTab("wiki");
		ModelAndView mv = new ModelAndView("/wiki/index", Constants.FORM_COMMAND_KEY, form);
		return mv;
	}

	/**
	 * 查看知识库的文件夹分类
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/view")
	public ModelAndView wiki_view(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/view_list", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String folderId = StringUtil.StringFilter(form.getFolderId());
		form.setFolderId(folderId);
		// 2.查询
		List<TcaKnowledgefolder> list = wikiService.selectFloderAndTitle(folderId);
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 查看知识库的具体的条目，查看文章内容
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/view/item")
	public ModelAndView wiki_view_item(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/item", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String folderId = StringUtil.StringFilter(form.getFolderId());
		String itemId = StringUtil.StringFilter(form.getItemId());
		form.setFolderId(folderId);
		form.setItemId(itemId);
		// 2.查询
		TcaKnowledgefile item = wikiService.selectItemById(itemId);
		mv.addObject("item", item);
		return mv;
	}

	/**
	 * 全目录管理
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/catalog")
	public ModelAndView wiki_catalog(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/catalog", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String folderId = StringUtil.StringFilter(form.getFolderId());
		form.setFolderId(folderId);
		// 2.查询
		List<TcaKnowledgefolder> list = wikiService.selectAllFolder();
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 修改目录-页面
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/catalog/edit")
	public ModelAndView wiki_catalog_edit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		String folderId = form.getFolderId();
		TcaKnowledgefolder folder = wikiService.selectFolderById(folderId);
		Map map = new HashMap<String, String>();
		map.put("title", folder.getTitle());
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

	/**
	 * 修改目录-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/catalog/doEdit")
	public ModelAndView wiki_catalog_doEdit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		String folderId = form.getFolderId();
		String newTitle = form.getTitle().trim();
		int result = wikiService.updateFolderTitle(newTitle, folderId);
		Map map = new HashMap<String, String>();
		map.put("status", "SUCCESS");
		map.put("msg", "修改成功");
		map.put("newTitle", newTitle);
		map.put("sid", EncrypTool.encode(folderId));
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

	/**
	 * 增加子目录-页面
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/catalog/addSub")
	public ModelAndView wiki_catalog_addSub(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		// 不必返回任何数据
		Map map = new HashMap<String, String>();
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

	/**
	 * 添加子目录-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/catalog/doAddSub")
	public ModelAndView wiki_catalog_doAddSub(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		String folderId = form.getFolderId();
		String newTitle = form.getTitle();

		// 1.获取父级目录
		TcaKnowledgefolder folder = new TcaKnowledgefolder();
		folder.setTitle(newTitle);
		if ("-1".equalsIgnoreCase(folderId)) {
			folder.setParentid("-1");
			folder.setLevelid(0L);
		} else {
			TcaKnowledgefolder pFolder = wikiService.selectFolderById(folderId);
			folder.setParentid(pFolder.getFolderid());
			folder.setLevelid(pFolder.getLevelid() + 1);
		}
		int result = wikiService.updateAddFolder(folder);
		Map map = new HashMap<String, String>();
		map.put("status", "SUCCESS");
		map.put("msg", "修改成功");
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

	/**
	 * 增加条目-页面
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/add")
	public ModelAndView wiki_item_add(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/add_item", Constants.FORM_COMMAND_KEY, form);
		// 获取目录
		List<TcaKnowledgefolder> list = wikiService.selectAllFolder4Option();
		// 不必返回任何数据
		Map map = new HashMap<String, String>();
		mv.addObject("folderList", list);
		return mv;
	}

	/**
	 * 增加条目-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/doAdd")
	public ModelAndView wiki_item_doAdd(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/add_item_result", Constants.FORM_COMMAND_KEY, form);
		String title = form.getTitle();
		String folderId = form.getFolderId();
		String content = form.getContent();
		int result = wikiService.insertItem(title, folderId, content);
		mv.addObject("status", "SUCCESS");
		mv.addObject("msg", "添加成功");
		return mv;
	}

	/**
	 * 条目管理列表
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/manage")
	public ModelAndView wiki_item_manage(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/manage_item", Constants.FORM_COMMAND_KEY, form);
		List<TcaKnowledgefolder> folderList = wikiService.selectAllFolder4Option();
		mv.addObject("folderList", folderList);
		// 1.模糊搜索关键字
		String folderId = StringUtil.StringFilter(form.getFolderId());
		String title = StringUtil.StringFilter(form.getTitle());
		form.setFolderId(folderId);
		form.setTitle(title);
		if ("-1".equalsIgnoreCase(folderId)) {
			folderId = null;
		}

		// 2.总记录数
		int count = wikiService.selectItemsCount(title, folderId, null);
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则

		// 5.查询
		List<TcaKnowledgefile> list = wikiService.selectItems(begin, pageSize, null, null, title, folderId, null);
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 编辑条目
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/edit")
	public ModelAndView wiki_item_edit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/edit_item", Constants.FORM_COMMAND_KEY, form);
		List<TcaKnowledgefolder> folderList = wikiService.selectAllFolder4Option();
		mv.addObject("folderList", folderList);
		String id = form.getId();
		String realId = EncrypTool.dencode(id);
		TcaKnowledgefile item = wikiService.selectItemById(realId);
		mv.addObject("item", item);
		return mv;
	}

	/**
	 * 编辑条目-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/doEdit")
	public ModelAndView wiki_item_doEdit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/add_item_result", Constants.FORM_COMMAND_KEY, form);
		String title = form.getTitle();
		String folderId = form.getFolderId();
		String content = form.getContent();
		String itemId = form.getItemId();
		int result = wikiService.updateItem(title, folderId, itemId, content);
		mv.addObject("status", "SUCCESS");
		mv.addObject("msg", "编辑成功");
		return mv;
	}

	/**
	 * 删除条目-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/doDelete")
	public ModelAndView wiki_item_doDelete(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		String id = form.getId();
		String realId = EncrypTool.dencode(id);
		wikiService.deleteItem(realId);
		Map map = new HashMap<String, String>();
		map.put("status", "SUCCESS");
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

	/**
	 * 文章搜索页
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/view/search")
	public ModelAndView wiki_view_search(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/view_search", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String folderId = StringUtil.StringFilter(form.getFolderId());
		String title = StringUtil.StringFilter(form.getTitle());
		form.setFolderId(folderId);
		form.setTitle(title);
		if ("-1".equalsIgnoreCase(folderId)) {
			folderId = null;
		}

		// 2.总记录数
		int count = wikiService.selectItemsCount(title, folderId, "1");
		if (count == 0) {
			return mv;
		}

		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE_WIKI_SEARCH, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则

		// 5.查询
		List<TcaKnowledgefile> list = wikiService.selectItems(begin, pageSize, null, null, title, folderId, "1");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 审核页面
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/audit")
	public ModelAndView wiki_item_audit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/audit", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String title = null;
		String folderId = null;
		// 2.总记录数
		int count = wikiService.selectItemsCount(title, folderId, "0");
		if (count == 0) {
			return mv;
		}
		// 3.分页信息
		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();

		// 4.排序规则

		// 5.查询
		List<TcaKnowledgefile> list = wikiService.selectItems(begin, pageSize, null, null, title, folderId, "0");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 审核预览
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/audit/view")
	public ModelAndView wiki_item_audit_view(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/wiki/item_audit", Constants.FORM_COMMAND_KEY, form);
		// 1.模糊搜索关键字
		String itemId = StringUtil.StringFilter(form.getItemId());
		form.setItemId(itemId);

		// 2.总记录数

		// 3.分页信息

		// 4.排序规则

		// 5.查询
		TcaKnowledgefile item = wikiService.selectItemById(itemId);
		mv.addObject("item", item);
		return mv;
	}

	/**
	 * 审核-执行
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/doAudit")
	public ModelAndView wiki_item_doAudit(HttpServletRequest request, HttpServletResponse response, WikiForm form)
			throws Exception {
		String itemId = form.getItemId();
		String realItemId = EncrypTool.dencode(itemId);
		String auditStatus = form.getAuditStatus();
		if ("1".equalsIgnoreCase(auditStatus)) {
			auditStatus = "1";
		} else {
			auditStatus = "-1";
		}
		int result = wikiService.updateAudit(realItemId, auditStatus);
		Map map = new HashMap<String, String>();
		map.put("status", "SUCCESS");
		map.put("msg", "审核成功");
		ModelAndView mv = new ModelAndView(new XFMappingJacksonJsonView(), map);
		return mv;
	}

}
